<template>
	<view class="calendar-content" v-if="showCalendar">
		<!-- <text class="example-info">日历组件可以查看日期，选择任意范围内的日期，打点操作。常用场景如：酒店日期预订、火车机票选择购买日期、上下班打卡等。</text> -->
		<!-- <uni-section title="插入模式" type="line"></uni-section> -->
		<view>
			<!-- 插入模式 -->
			<uni-calendar :selected="info.selected" :showMonth="false" @change="change" @monthSwitch="monthSwitch" />
		</view>
		<!-- <uni-section title="弹出模式" type="line"></uni-section> -->
		<!-- <view class="example-body">
			<button class="calendar-button" type="button" @click="open">打开日历</button>
		</view> -->
		<uni-calendar ref="calendar" :clear-date="true" :date="info.date" :insert="info.insert" :lunar="info.lunar"
			:startDate="info.startDate" :endDate="info.endDate" :range="info.range" @confirm="confirm" @close="close" />
	</view>
</template>

<script>
	/**
	 * 获取任意时间
	 */
	function getDate(date, AddDayCount = 0) {
		console.log(date)
		console.log(date)
		
		if (!date) {
			date = new Date()
		}
		console.log(2222)
		if (typeof date !== 'object') {
			date = date.replace(/-/g, '/')
		}
		const dd = new Date(date)

		dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

		const y = dd.getFullYear()
		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
		const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
		return {
			fullDate: y + '-' + m + '-' + d,
			year: y,
			month: m,
			date: d,
			day: dd.getDay()
		}
	}

	//两个时间相差天数 兼容firefox chrome
	function datedifference(sDate1, sDate2) { //sDate1和sDate2是2006-12-18格式  
		var dateSpan,
			tempDate,
			iDays;
		sDate1 = Date.parse(sDate1);
		sDate2 = Date.parse(sDate2);
		dateSpan = sDate2 - sDate1;
		dateSpan = Math.abs(dateSpan);
		iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
		return iDays
	};

	export default {
		components: {},
		data() {
			return {
				showCalendar: false,
				info: {
					lunar: true,
					range: true,
					insert: false,
					selected: []
				},
				free_date: '2021-11-13'//休息
			}
		},
		onReady() {
			this.$nextTick(() => {
				this.showCalendar = true
			})
			
				// this.info.date = getDate(new Date(), -30).fullDate
				// this.info.startDate = getDate(new Date(), -60).fullDate
				// this.info.endDate = getDate(new Date(), 30).fullDate
			
				for (var i = 1; i < 180; i++) {
										
					var tmp = getDate(new Date(), -i).fullDate

					var num = datedifference(this.free_date, tmp);

					switch (num % 3) {
						case 0:
							var obj = {
								date: getDate(new Date(), -i).fullDate,
								info: '休'
							};
							break;
						case 1:
							var obj = {
								date: getDate(new Date(), -i).fullDate,
								info: '夜'
							};
							break;
						case 2:
							var obj = {
								date: getDate(new Date(), -i).fullDate,
								info: '白'
							};
							break;
					}

					this.info.selected.push(obj);

				}
				
				for (var i = 1; i < 180; i++) {
										
					var tmp = getDate(new Date(), i).fullDate
				
					var num = datedifference(this.free_date, tmp);
				
					switch (num % 3) {
						case 0:
							var obj = {
								date: getDate(new Date(), i).fullDate,
								info: '休'
							};
							break;
						case 1:
							var obj = {
								date: getDate(new Date(), i).fullDate,
								info: '白'
							};
							break;
						case 2:
							var obj = {
								date: getDate(new Date(), i).fullDate,
								info: '夜'
							};
							break;
					}
				
					this.info.selected.push(obj);
				
				}
				
			
		},
		methods: {
			open() {
				this.$refs.calendar.open()
			},
			close() {
				console.log('弹窗关闭');
			},
			change(e) {
				console.log('change 返回:', e)
				// 模拟动态打卡
				// if (this.info.selected.length > 5) return
				// this.info.selected.push({
				// 	date: e.fulldate,
				// 	info: '打卡'
				// })
			},
			confirm(e) {
				console.log('confirm 返回:', e)
			},
			monthSwitch(e) {
				console.log('monthSwitchs 返回:', e)
			},

		}
	}
</script>

<style>
	/* 头条小程序组件内不能引入字体 */
	/* #ifdef MP-TOUTIAO */
	@font-face {
		font-family: uniicons;
		font-weight: normal;
		font-style: normal;
		src: url('~@/static/uni.ttf') format('truetype');
	}

	/* #endif */

	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #efeff4;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	.example {
		padding: 0 15px 15px;
	}

	.example-info {
		padding: 15px;
		color: #3b4144;
		background: #ffffff;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: center;
		padding: 0;
		font-size: 14px;
		background-color: #ffffff;
	}

	/* #endif */
	.example {
		padding: 0 15px;
	}

	.example-info {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		padding: 15px;
		color: #3b4144;
		background-color: #ffffff;
		font-size: 14px;
		line-height: 20px;
	}

	.example-info-text {
		font-size: 14px;
		line-height: 20px;
		color: #3b4144;
	}


	.example-body {
		flex-direction: column;
		padding: 15px;
		background-color: #ffffff;
	}

	.word-btn-white {
		font-size: 18px;
		color: #FFFFFF;
	}

	.word-btn {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		border-radius: 6px;
		height: 48px;
		margin: 15px;
		background-color: #007AFF;
	}

	.word-btn--hover {
		background-color: #4ca2ff;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	.calendar-button {
		flex: 1;
		font-weight: bold;
		font-size: 32rpx;
	}
</style>
